package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.job.AddJobRequest;
import com.springboot_gradrecruit.model.dto.job.PageJobRequest;
import com.springboot_gradrecruit.model.dto.job.UpdateJobRequest;
import com.springboot_gradrecruit.model.entity.Job;
import com.springboot_gradrecruit.model.vo.enterprise.EnterpriseVO;
import com.springboot_gradrecruit.model.vo.job.JobDetailVO;
import com.springboot_gradrecruit.model.vo.job.JobVO;
import com.springboot_gradrecruit.service.EnterpriseService;
import com.springboot_gradrecruit.service.JobService;
import com.springboot_gradrecruit.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author zr
 */
@Tag(name = "岗位相关接口")
@RestController
@RequestMapping("/job")
@Slf4j
public class JobController {

    @Resource
    private JobService jobService;

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private UserService userService;


    //1.添加职位
    @Operation(summary = "添加职位")
    @PostMapping("/addJob")
    public R<Boolean> addJob(@RequestBody AddJobRequest addJobRequest, HttpServletRequest request){
        //1. 参数校验
        if(addJobRequest == null||request==null){
            throw new RuntimeException("参数为空");
        }
        //2.. 调用service完成添加
        Long aLong = jobService.addJob(addJobRequest, request);
        if(aLong>0){
            return R.success(true, "添加成功");
        }
        return R.error(false, "添加失败");

    }

    //2.分页获取职位
    @Operation(summary = "分页获取职位")
    @PostMapping("/pageJob")
    public R<PageResponse<JobVO>> pageJob(@RequestBody PageJobRequest pageJobRequest, HttpServletRequest request){
        //1. 参数校验
        if(pageJobRequest==null||request==null){
            throw new RuntimeException("参数为空");
        }
       // 2. 调用service完成查询
        PageResponse<JobVO> pageJob=jobService.pageJob(pageJobRequest);
        return R.success(pageJob, "查询成功");
    }

    //3.更新职位
    @Operation(summary = "更新职位")
    @PostMapping("/updateJobById")
    public R<Boolean> updateJobById(@RequestBody UpdateJobRequest updateJobRequest, HttpServletRequest request) {
        //1. 参数校验
        if (updateJobRequest == null || request == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        boolean updateJob = jobService.updateJobById(updateJobRequest);
        if (updateJob) {
            return R.success(null, "修改成功");
        }
        return R.error(null, "修改失败");
    }

    //4.删除职位
    @Operation(summary = "删除职位")
    @PostMapping("/deleteJob")
    public R<Boolean> deleteJob(Long jobId){
        //1. 参数校验
        if(jobId==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成删除
        boolean b = jobService.removeById(jobId);
          return b?R.success(true,"删除成功"):R.error(false,"删除失败");
    }

    //5.获取职位详情根据id
    @Operation(summary = "获取职位详情根据id")
    @PostMapping("/queryJobDetailByJobId")
    public R<JobDetailVO> queryJobDetailByJobId(Long jobId){
        //1. 参数校验
        if(jobId==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        JobDetailVO jobDetailVO = new JobDetailVO();
        Job job = jobService.getById(jobId);
        //判断岗位状态
        Date jobDeadline = job.getJobDeadline();
        if(jobDeadline.before(new Date())){
            job.setJobStatus("关闭");
            jobService.updateById(job);
        }
        Long enterpriseId = job.getJobCompanyId();
        EnterpriseVO enterpriseVO=BeanUtil.copyProperties(enterpriseService.getById(enterpriseId),EnterpriseVO.class);
        JobVO jobVO= BeanUtil.copyProperties(job, JobVO.class);
        jobDetailVO.setJobVO(jobVO);
        jobDetailVO.setEnterpriseVO(enterpriseVO);
        return R.success(jobDetailVO, "查询成功");
    }

    //6.更改岗位管理状态（admin)
    @Operation(summary = "管理员-更改岗位管理状态")
    @PostMapping("/updateJobStatus")
    public R<Boolean> updateJobStatus(Long jobId,Integer jobAdminStatus) {
        //1. 参数校验
        if(jobId==null||jobAdminStatus==null){
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        try {
            // 先从岗位服务中根据岗位ID获取企业相关信息
            Job job = jobService.getById(jobId);
            if (job == null) {
                return R.error(null, "找不到对应的岗位记录");
            }
            // 3. 修改岗位-管理状态
            job.setJobAdminStatus(jobAdminStatus);
            boolean updateJobResult = jobService.updateById(job);
            if (!updateJobResult) {
                return R.error(null, "更新岗位状态失败");
            }
            return R.success(null, "更新成功");
        } catch (Exception e) {
            log.error("更新用户和企业状态出现异常", e);
            return R.error(null, "更新出现异常");
        }
    }

    //7.管理员获取岗位信息
    @Operation(summary = "管理员获取岗位信息")
    @PostMapping("/queryJobByJobId")
    public R<JobVO> queryJobByJobId(Long jobId){
        //1. 参数校验
        if(jobId==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        Job job = jobService.getById(jobId);
        JobVO jobVO= BeanUtil.copyProperties(job, JobVO.class);
        return R.success(jobVO, "查询成功");
    }

    //8.学生首页获取热招岗位
    @Operation(summary = "学生首页获取热招岗位")
    @PostMapping("/queryHotJob")
    public R<List<JobVO>> queryHotJob(){
        //1. 调用service完成查询，根据岗位投递人数来排行
        List<JobVO> jobVOList=jobService.queryHotJob();
        return R.success(jobVOList, "查询成功");
    }


    //9.获取企业的岗位
    @Operation(summary = "获取企业的岗位")
    @PostMapping("/queryJobByEnterpriseId")
    public R<List<JobVO>> queryJobByEnterpriseId(Long enterpriseId){
        //1. 参数校验
        if(enterpriseId==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        List<JobVO> jobVOList=jobService.queryJobByEnterpriseId(enterpriseId);
        return R.success(jobVOList,"查询成功");
    }

    //10.获取期望行业的全部岗位
    @Operation(summary = "获取期望行业的全部岗位")
    @PostMapping("/queryJobByJobIndustry")
    public R<List<JobVO>> queryJobByJobIndustry(@RequestBody List<String> jobIndustry){
        //1. 参数校验
        if(jobIndustry==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        List<JobVO> jobVOList=jobService.queryJobByJobIndustry(jobIndustry);
        return R.success(jobVOList,"查询成功");
    }

    //11.获取期望职位的全部岗位
    @Operation(summary = "获取期望职位的全部岗位")
    @PostMapping("/queryJobByJobPosition")
    public R<List<JobVO>> queryJobByJobPosition(@RequestBody List<String> jobPosition){
        //1. 参数校验
        if(jobPosition==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        List<JobVO> jobVOList=jobService.queryJobByJobPosition(jobPosition);
        return R.success(jobVOList,"查询成功");
    }

    //12.获取企业的岗位标题
    @Operation(summary = "获取企业的岗位标题")
    @PostMapping("/queryJobTitleByEnterpriseId")
    public R<List<String>> queryJobTitleByEnterpriseId(Long enterpriseId){
        //1. 参数校验
        if(enterpriseId==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        List<String> jobTitleList=jobService.queryJobTitleByEnterpriseId(enterpriseId);
        return R.success(jobTitleList,"查询成功");
    }

    //13.获取企业的岗位的所属行业
    @Operation(summary = "获取企业的岗位的所属行业")
    @PostMapping("/queryJobIndustryByEnterpriseId")
    public R<List<String>> queryJobIndustryByEnterpriseId(Long enterpriseId){
        //1. 参数校验
        if(enterpriseId==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        List<String> jobIndustryList=jobService.queryJobIndustryByEnterpriseId(enterpriseId);
        return R.success(jobIndustryList,"查询成功");
    }

}
